﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElectrosLTD.Models;
using Common;
using ElectrosLTD.BarcodeServ;
using System.Drawing;

namespace ElectrosLTD.Controllers
{
    public class FaultController : Controller
    {
        //
        // GET: /Fault/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Faults()
        {
            List<Fault> fault = new List<Fault>();
            List<FaultListModel> flmList = new List<FaultListModel>();

            foreach (Fault f in fault)
            {
                FaultListModel flm = new FaultListModel();
                OrderDetail od = new ProductServ.ProductWCFClient().GetOrderDetail(f.orderDetailID);
                Product p = new ProductServ.ProductWCFClient().GetProductById(od.ProductId_FK);
                
                flm.ID = f.ID;
                flm.OrderDetailsID = od.ID;
                flm.productName = p.Name;
                flm.username = f.Username;
                flm.faultDescription = f.Description;
                flm.EnteredDate = f.EnteredDate;
                flm.ExpiryDate = f.ExpiredDate;
                flm.Status = f.FaultStatu.Status;
                flmList.Add(flm);
            }

            return View("Faults", flmList);
        }

        public ActionResult FaultSubmit(int orderDetailsID)
        {
            OrderDetail od = new ProductServ.ProductWCFClient().GetOrderDetail(orderDetailsID);
            Product p = new ProductServ.ProductWCFClient().GetProductById(od.ProductId_FK);
            FaultSubmitModel fsm = new FaultSubmitModel();

            fsm.orderDetailsID = od.ID.ToString();
            fsm.productName = p.Name;
            fsm.Description = "";

            return View("~/Views/Fault/FaultSubmit.cshtml", fsm);
        }

        [HttpPost]
        public ActionResult FaultSubmit(FaultSubmitModel fsm)
        {
            if (new UserServ.UserWCFClient().UserAuthentication(fsm.username, new ElectrosServ.ElectrosWCFClient().DecryptPassword(fsm.EncryptedPassword, fsm.pin)) == true)
            {
                Fault f = new Fault();
                OrderDetail od = new ProductServ.ProductWCFClient().GetOrderDetail(Convert.ToInt32(fsm.orderDetailsID));

                f.orderDetailID = Convert.ToInt32(fsm.orderDetailsID);
                f.Description = fsm.Description;
                f.ExpiredDate = od.Order.PurchaseDate.AddYears(2);
                f.StatusID = 1;
                f.Username = fsm.username;
                f.EnteredDate = DateTime.Today;
                f.CategoryID_FK = fsm.categoryID;
                new FaultServ.FaultWCFClient().AddToFaults(f);

                int randomGenerator = new ElectrosServ.ElectrosWCFClient().RandomGenerator();
                //pass the barcode and the random ticket

                User u = new UserServ.UserWCFClient().GetUserByUsername(fsm.username);
                new ElectrosServ.ElectrosWCFClient().SendEmail(u.Email,fsm.username, f);
            }

            return View("Order", "Product");
            
        }

        public ActionResult FaultUpdation(int FaultId)
        {
            IEnumerable<Fault> faultList = new FaultServ.FaultWCFClient().GetAllFaults();
            FaultListModel flm = new FaultListModel();

            foreach (Fault f in faultList)
            {
                if (f.ID == FaultId)
                {
                    Product p = new ProductServ.ProductWCFClient().GetProductById(f.OrderDetail.ProductId_FK);
                    flm.ID = f.ID;
                    flm.OrderDetailsID = f.orderDetailID;
                    flm.productName = p.Name;
                    FaultStatu s = new FaultServ.FaultWCFClient().GetFaultStatus(f.StatusID);
                    flm.Status = f.FaultStatu.Status;
                    flm.username = f.Username;
                    flm.faultDescription = f.Description;
                    flm.EnteredDate = f.EnteredDate;
                    flm.ExpiryDate = f.ExpiredDate;
                    flm.faulStatuses = new SelectList(new FaultServ.FaultWCFClient().GetAllFaultStatus(),"ID", "Status");
                }
            }

            return View("FaultUpdation", flm);
        }

        [HttpPost]
        public ActionResult FaultUpdation(FaultListModel flm)
        {
            //re-settings the fault details
            Fault f = new FaultServ.FaultWCFClient().GetFaultByID(flm.ID);
            f.StatusID = Convert.ToInt32(flm.Status);
            
            //re-setting the status in the order detail
            OrderDetail od = new ProductServ.ProductWCFClient().GetOrderDetail(flm.OrderDetailsID);
            od.Status_FK = Convert.ToInt32(flm.Status);

            //saving the details
            new ProductServ.ProductWCFClient().UpdateOrderDetail(od);
            new FaultServ.FaultWCFClient().UpdateFault(f);

            //gets the user of that fault and sends an email in order to notify
            User user = new UserServ.UserWCFClient().GetUserByUsername(f.Username);
            new SMSServ.SMSWCFClient().sendSMS(user.Mobile.ToString());
            return Redirect("~/Fault/Faults");
        }
    }
}
